package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwefT;
import cn.e4j.modular.crm.mapper.Gwei007Mapper;
import cn.e4j.modular.crm.service.Gwei007Service;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class Gwei007ServiceImpl extends ServiceImpl<Gwei007Mapper, GwefT> implements Gwei007Service {
    @Override
    public void addData(List<GwefT> list) {
        ShiroUser shiroUser = ShiroKit.getUser();

        for (int i = 0; i < list.size(); i++) {
            GwefT gwef = list.get(i);
            gwef.setGWEFENT(shiroUser.getDBC());
            gwef.setGWEFLEGAL(gwef.getGWEFPLANT());
            gwef.setGWEF022("0");
            gwef.setGWEFCONF("N");
            gwef.insert();
        }
    }

    @Override
    public void  editData(List<GwefT> list) {
        ShiroUser shiroUser = ShiroKit.getUser();
        String DBC = shiroUser.getDBC();
        UpdateWrapper<GwefT> gwefTUpdateWrapper = new UpdateWrapper<>();
        gwefTUpdateWrapper.eq("GWEFENT", DBC);
        gwefTUpdateWrapper.eq("GWEFDOCNO", list.get(0).getGWEFDOCNO());
        list.get(0).delete(gwefTUpdateWrapper);

        for (int i = 0; i < list.size(); i++) {
            GwefT gwef = list.get(i);
            gwef.setGWEFENT(shiroUser.getDBC());
            gwef.insert();
        }
    }

    @Override
    public void review(List<GwefT> list) {
        ShiroUser shiroUser = ShiroKit.getUser();

        String ENT = shiroUser.getDBC();
        for (int i = 0; i < list.size(); i++) {
            GwefT updateGwef = new GwefT();
            UpdateWrapper<GwefT> gwefTUpdateWrapper = new UpdateWrapper<>();
            String DOCNO = list.get(i).getGWEFDOCNO();

            updateGwef.setGWEFCONF("Y");
            gwefTUpdateWrapper.eq("GWEFENT", ENT);
            gwefTUpdateWrapper.eq("GWEFDOCNO", DOCNO);
            updateGwef.update(gwefTUpdateWrapper);
        }
    }

    @Override
    public void cancelReview(List<GwefT> list) {
        ShiroUser shiroUser = ShiroKit.getUser();

        String ENT = shiroUser.getDBC();
        for (int i = 0; i < list.size(); i++) {
            GwefT updateGwef = new GwefT();
            UpdateWrapper<GwefT> gwefTUpdateWrapper = new UpdateWrapper<>();
            String DOCNO = list.get(i).getGWEFDOCNO();

            updateGwef.setGWEFCONF("N");
            gwefTUpdateWrapper.eq("GWEFENT", ENT);
            gwefTUpdateWrapper.eq("GWEFDOCNO", DOCNO);
            updateGwef.update(gwefTUpdateWrapper);
        }
    }

    @Override
    public void delete(GwefT gwefT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        UpdateWrapper<GwefT> gwefTUpdateWrapper = new UpdateWrapper<>();
        gwefTUpdateWrapper.eq("GWEFENT", shiroUser.getDBC());
        gwefTUpdateWrapper.eq("GWEFDOCNO", gwefT.getGWEFDOCNO());
        gwefTUpdateWrapper.eq("GWEFSEQ", gwefT.getGWEFSEQ());
        gwefT.delete(gwefTUpdateWrapper);
    }

    @Override
    public void deleteBatch(List<GwefT> list) {
        for (int i = 0; i < list.size(); i++) {
            this.delete(list.get(i));
        }
    }

    @Override
    public List<Dto> getList(Page<Object> page, Dto inDto) {
        ShiroUser user = ShiroKit.getUser();
        String ID = user.getID();
        String LANG = LangUtils.getLang(user.getLANG());
        String ENT = user.getDBC();
        inDto.put("USER_ID",ID);
        inDto.put("LANG",LANG);
        inDto.put("ENT",ENT);
//        if(StringUtils.isNotEmpty(user.getDEVICEID())){
//            inDto.put("has_device_id",true);
//        } else {
//            inDto.put("has_device_id",false);
//        }
//        return this.baseMapper.getList(page,inDto);
        return InvokeUtil.invokeBeanMethod("gwei007Mapper,getList",page,inDto);
    }

    @Override
    public List<Dto> getGwefdocnoListPage(Page<Object> page, Dto dto) {
//        return this.baseMapper.getGwefdocnoListPage(page,dto);
        return InvokeUtil.invokeBeanMethod("gwei007Mapper,getGwefdocnoListPage",page,dto);
    }

    @Override
    public List<Dto> getGwef002ListPage(Page<Object> page, Dto dto) {
        return this.baseMapper.getGwef002ListPage(page,dto);
    }

    @Override
    public List<Dto> getGwedListPage(Page<Object> page, Dto dto) {
//        return this.baseMapper.getGwedListPage(page,dto);
        return InvokeUtil.invokeBeanMethod("gwei007Mapper,getGwedListPage",page,dto);
    }

    @Override
    public List<Dto> getGwakPage(Page<Object> page, Dto dto) {
        return this.baseMapper.getGwakPage(page,dto);
    }

    @Override
    public List<Dto> getGweaPage(Page<Object> page, Dto dto) {
        return this.baseMapper.getGweaPage(page,dto);
    }

    @Override
    public List<Dto> getGwefData(Dto dto) {
//        return this.baseMapper.getGwefData(dto);
        return InvokeUtil.invokeBeanMethod("gwei007Mapper,getGwefData",dto);
    }


    @Override
    public Dto getGwak008(Dto dto) {
        return this.baseMapper.getGwak008(dto);
    }

    @Override
    public Dto getGwak012(Dto dto) {
        return this.baseMapper.getGwak012(dto);
    }

    @Override
    public Dto getSysParams(Dto dto) {
        return this.baseMapper.getSysParams(dto);
    }

    @Override
    public Dto getGwek005(Dto dto) {
        return this.baseMapper.getGwek005(dto);
    }

    @Override
    public Dto getGwak057(Dto dto) {
        return this.baseMapper.getGwak057(dto);
    }

    @Override
    public Dto getGwel005(Dto dto) {
        return this.baseMapper.getGwel005(dto);
    }

    public Dto getGwef019(Dto inDto) {
        ShiroUser shiroUser = ShiroKit.getUser();

        inDto.put("ENT", shiroUser.getDBC());
        Dto outDto = Dtos.newDto();
        Dto aDto = this.baseMapper.getGwak057(inDto);
        if(aDto.size() > 0){
            outDto.put("GWEF019",aDto.getString("GWAK057"));
        }else {
            Dto bDto = this.baseMapper.getSysParams(inDto);
            if(bDto.size() > 0){
                outDto.put("GWEF019",bDto.getString("VALUE"));
            }
        }
        return outDto;
    }

    public Dto getGwef007(Dto inDto) {
        ShiroUser shiroUser = ShiroKit.getUser();

        inDto.put("ENT", shiroUser.getDBC());
        Dto outDto = Dtos.newDto();
        if(inDto.getString("TYPE").equals( "1")){
            inDto.put("GWEK002", DateUtils.getDate("yyyyMMdd"));
            Dto aDto = this.getGwel005(inDto);

            if(aDto.size() > 0){
                outDto.put("GWEF007",aDto.getString("GWEL005"));
            }else {
                outDto.put("GWEF007","1");
            }
        } else {
            inDto.put("GWEK002", DateUtils.getDate("yyyyMM"));
            Dto bDto = this.getGwek005(inDto);
            if(bDto.size() > 0){
                outDto.put("GWEF007",bDto.getString("GWEK005"));
            }else {
                outDto.put("GWEF007","1");
            }
        }
        return outDto;
    }


    @Override
    public Dto getOea(Dto dto) {
        return this.baseMapper.getOea(dto);
    }


    @Override
    public Dto getAzpFile(Dto dto) {
        return this.baseMapper.getAzpFile(dto);
    }


    // 获取商品汇率，税率等信息
    public Dto getGwcsData(Dto dto) {
//        return this.baseMapper.getGwcsData(dto);
        return InvokeUtil.invokeBeanMethod("gwei007Mapper,getGwcsData",dto);
    }
}
